package cn.edu.sgu.www.mhxysy.controller.waiguan;

import cn.edu.sgu.www.mhxysy.consts.CustomConstants;
import cn.edu.sgu.www.mhxysy.consts.CustomMessages;
import cn.edu.sgu.www.mhxysy.consts.MimeTypes;
import cn.edu.sgu.www.mhxysy.dto.waiguan.ColorDTO;
import cn.edu.sgu.www.mhxysy.entity.waiguan.Color;
import cn.edu.sgu.www.mhxysy.restful.JsonResult;
import cn.edu.sgu.www.mhxysy.service.waiguan.ColorService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * @author 沐雨橙风ιε
 * @version 1.0
 */
@Api(tags = "颜色管理")
@ApiSupport(author = CustomConstants.AUTHOR)
@Validated
@RestController
@RequestMapping(path = "/color", produces = MimeTypes.APPLICATION_JSON_CHARSET_UTF_8)
public class ColorController {

    private final ColorService colorService;

    @Autowired
    public ColorController(ColorService colorService) {
        this.colorService = colorService;
    }

    @ApiOperation("添加颜色")
    @ApiOperationSupport(ignoreParameters = "id")
    @RequestMapping(value = "/insert", method = RequestMethod.POST)
    public JsonResult<Void> insert(@Validated ColorDTO colorDTO) {
        colorService.insert(colorDTO);

        return JsonResult.success(CustomMessages.INSERT_SUCCESS);
    }

    @ApiOperation("查询全部颜色")
    @RequestMapping(value = "/selectAll", method = RequestMethod.GET)
    public JsonResult<List<Color>> selectAll() {
        List<Color> list = colorService.selectAll();

        return JsonResult.success(null, list);
    }

    @ApiOperation("通过ID查询颜色")
    @RequestMapping(value = "/selectById", method = RequestMethod.GET)
    public JsonResult<Color> selectById(
            @ApiParam(name = "id", value = "颜色ID", required = true)
            @NotNull(message = CustomMessages.ID_NOT_NULL) Integer id) {
        Color color = colorService.selectById(id);

        return JsonResult.success(null, color);
    }

}